package com.kanshu.ksgb.fastread.doudou.common.business.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.view.CustomDialog;

/**
 * Created by xubo on 18/10/21
 */

public class CommonDialog extends Dialog implements View.OnClickListener{

    private TextView mContent;
    private TextView mSure;
    private TextView mCancel;
    private CustomDialog.Callback mCallback;
    private Activity mActivity;

    private CommonDialog(@NonNull Context context) {
        super(context, R.style.dialog_style);
        mActivity = (Activity) context;
        init();
    }

    public CommonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected CommonDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    public CommonDialog setContent(CharSequence s) {
        mContent.setText(s);
        return this;
    }

    public CommonDialog setSureText(String sureText){
        mSure.setText(sureText);
        return this;
    }

    public CommonDialog setCancelText(String cancelText){
        mCancel.setText(cancelText);
        return this;
    }

    public CommonDialog setCallback(CustomDialog.Callback callback) {
        mCallback = callback;
        return this;
    }

    private void init() {
        setContentView(R.layout.dialog_common);
        mContent = findViewById(R.id.dialog_content);
        mSure = findViewById(R.id.dialog_sure);
        mCancel = findViewById(R.id.dialog_cancel);
        mSure.setOnClickListener(this);
        mCancel.setOnClickListener(this);
    }


    public static CommonDialog show(Activity activity, CharSequence content, CustomDialog.Callback callback) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        final CommonDialog dialog = new CommonDialog(activity);
        dialog.setContent(content).setCallback(callback);
        dialog.show();
        return dialog;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_sure:
                if (mCallback != null) {
                    mCallback.onSure(this);
                } else {
                    dismiss();
                }
                break;
            case R.id.dialog_cancel:
                if (mCallback != null) {
                    mCallback.onCancel(this);
                } else {
                    dismiss();
                }
                break;
        }
    }

    @Override
    public void dismiss() {
        if (mActivity != null && !mActivity.isFinishing()) {
            super.dismiss();
        }
    }
}
